//取文件后缀名
function getFileExt(filepath)
{
    if (filepath != "") {
        var pos = filepath.replace(/.+\./, "");
        return pos;
    }
    return '';
}
//取文件名不带后缀
function getFileNameNoExt(filepath)
{
	var arr = filepath.split('.');
	return arr[0];
}

function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
	  	for(var i in haystack) {
	   		if(haystack[i] == needle) {
	     		return true;
	   		}
		}
	}
	return false;
}

String.prototype.replaceAll = function(s1,s2) { 
    /*var str = this;
	while (str.indexOf(s1) != -1) {
		str = str.replace(s1, s2);
	}
	return str;*/
	
	s1 = s1.replace(/(\[)/g, "\\[");
	s1 = s1.replace(/(\])/g, "\\]");
	s1 = s1.replace(/(\()/g, "\\(");
	s1 = s1.replace(/(\))/g, "\\)");
	s1 = s1.replace(/(\*)/g, "\\*");
	
	var ret=  this.replace(new RegExp(s1, 'gm'),s2);
	return ret;
}

Array.prototype.hasVal = function(e) { 
    for(i=0; i<this.length; i++){
        if(this[i] == e){
			return true;
		}
    }
    return false;
}

String.prototype.trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
String.prototype.lTrim = function() 
{ 
	return this.replace(/(^\s*)/g, ""); 
} 
String.prototype.rTrim = function() 
{ 
	return this.replace(/(\s*$)/g, ""); 
} 


$.extend($.prototype, {
	htmlCode:function(){
		var div = $('<div />');
		$(this).clone().appendTo(div);
		return div.html();
	}
}); 


$(function(){	
	$("#menubar").accordion({
		header: "h4",
		autoHeight: true,
		navigation: true
	});
});

/**
 * @author Maowl
 * 
 * 根据回传的json_endo形式的$_POST, 将值写回表单
 * 
 * usage:
 	var FormInitModel = new FormInit(<?php echo json_encode($_POST); ?>);
	FormInitModel.transverArrtoForm();
	
	如果$_POST中有数组形式的值，表单元素需要一个和name相同的class，如: <input name="group[]" class="group" />
 */
window.FormInit = function(_formData){
	this.formData = _formData;
	this.formGroup = null;
	
	this.TransverArrFunc = function (Arr, Arrvalname){
		var existCount = jQuery('.'+Arrvalname).size();
		var thisObj = this;
		
		jQuery.each(Arr, function(name, val){
			if(typeof(val) == 'object'){
				thisObj.TransverArrFunc(val, name);
			}else{
				if(Arrvalname != null){
					if(thisObj.formData['clickId'][Arrvalname] != null){
						if(name >= existCount-1){
							jQuery('#'+thisObj.formData['clickId'][Arrvalname]).click();
						}
					}
				}
				
				if(Arrvalname == null){
					if(thisObj.formGroup != null){
						name = thisObj.formGroup+'['+name+']';
					}
					jQuery('[name="'+name+'"]').val(val);
				}else{
					jQuery('.'+Arrvalname).eq(name).val(val);
				}
				
			}
		});
	};
	
	this.setFormGroup = function(_formGroup){
		this.formGroup = _formGroup;
	};
	
	this.transverArrtoForm = function(){
		this.TransverArrFunc(this.formData, null);
	}
};




